//********************************************************************************************************
// Product Name: MapWindow.dll Alpha
// Description:  The basic module for MapWindow version 6.0
//********************************************************************************************************
// The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
// you may not use this file except in compliance with the License. You may obtain a copy of the License at 
// http://www.mozilla.org/MPL/ 
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 
// ANY KIND, either expressed or implied. See the License for the specificlanguage governing rights and 
// limitations under the License. 
//
// The Original Code is from MapWindow.dll version 6.0
//
// The Initial Developer of this Original Code is Ted Dunsford. Created 6/12/2008 4:15:44 PM
// 
// Contributor(s): (Open source contributors should list themselves and their modifications here). 
//
//********************************************************************************************************

using System.Collections.Generic;
using System.Drawing;
using MapWindow.Drawing;
using MapWindow.Geometries;
using Microsoft.DirectX.Direct3D;

namespace MapWindow.DirectX
{


    /// <summary>
    /// EnvelopeDX
    /// </summary>
    public class EnvelopeDX : RenderBaseDX
    {
        #region Private Variables

       
        private PolygonSymbolizer _symbolizer;
      

        private PositionColoredVC _borderVertices;
        private PositionColoredVC _fillVertices;
        private IndexCollection _fillIndices;
        private IndexCollection _borderIndices;
      
        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new instance of EnvelopeDX
        /// </summary>
        public EnvelopeDX()
        {
            _symbolizer = new PolygonSymbolizer();

        }

        /// <summary>
        /// Just the boundaries
        /// </summary>
        /// <param name="borderColor"></param>
        /// <param name="envelope"></param>
        public EnvelopeDX(Color borderColor, IEnvelope envelope)
        {
            _symbolizer = new PolygonSymbolizer();
            _symbolizer.OutlineSymbolizer.SetFillColor(borderColor);
            _symbolizer.SetFillColor(Color.Transparent);
            base.Envelope = envelope;
          
        }

        /// <summary>
        ///  Constructs A DirectX drawing entity from an envelope
        /// </summary>
        /// <param name="borderColor">The outside border color of the envelope</param>
        /// <param name="envelope">The MapWindow.Drawing.Geometries of the envelope itself</param>
        /// <param name="fillColor">The fill color</param>
        public EnvelopeDX(Color borderColor, IEnvelope envelope, Color fillColor)
        {
            _symbolizer = new PolygonSymbolizer();
            _symbolizer.OutlineSymbolizer.SetFillColor(borderColor);
            _symbolizer.SetFillColor(fillColor);
            base.Envelope = envelope;
           
        }

        #endregion

        #region Methods

        #endregion

        #region Properties



        #endregion




        #region IRenderableDX Members



        /// <summary>
        /// This is the internal drawing method that follows the standard checks.  Drawing is evoked
        /// through the Draw3D command, but internally implemented in the DrawPart method.  After
        /// the initial checks, the Draw3D method will cycle the part integer from 0 to NumParts -1.
        /// </summary>
        /// <param name="args">A DrawArgsDX class with the device and camera.</param>
        protected override void OnDraw(DrawArgsDX args)
        {
         
            
            // Draw border lines
            if (_symbolizer.OutlineSymbolizer.IsVisible)
            {
                _borderVertices.BeforeDrawing(args.Device);
                _borderIndices.BeforeDrawing(args.Device);
                args.Device.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, 4, 0, 4);
                _borderIndices.AfterDrawing(args.Device);
                _borderVertices.AfterDrawing(args.Device);
            }
            if (_symbolizer != null && _symbolizer.GetFillColor() != Color.Transparent)
            {
                _fillIndices.BeforeDrawing(args.Device);
                _fillVertices.BeforeDrawing(args.Device);
                args.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
                _fillIndices.AfterDrawing(args.Device);
                _fillVertices.AfterDrawing(args.Device);
            }

         
            
        }

        /// <summary>
        /// Initializes this envelope
        /// </summary>
        public override void  Initialize(DrawArgsDX args)
        {
            _borderVertices = new PositionColoredVC();
            _borderVertices.DrawBox = args.Camera.DrawBox;
            _fillVertices = new PositionColoredVC();
            _fillVertices.DrawBox = args.Camera.DrawBox;
            Coordinate topLeft = new Coordinate(Envelope.Minimum.X, Envelope.Maximum.Y, 0);
            Coordinate topRight = new Coordinate(Envelope.Maximum.X, Envelope.Maximum.Y, 0);
            Coordinate bottomLeft = new Coordinate(Envelope.Minimum.X, Envelope.Minimum.Y, 0);
            Coordinate bottomRight = new Coordinate(Envelope.Maximum.X, Envelope.Minimum.Y, 0);
            
            // Border
            Color borderColor = Color.Red;
            if (_symbolizer != null)
            {
                borderColor = _symbolizer.OutlineSymbolizer.GetFillColor();
            }
            _borderVertices.Color = borderColor;
            _borderVertices.AddCoordinate(topLeft);
            _borderVertices.AddCoordinate(topRight);
            _borderVertices.AddCoordinate(bottomLeft);
            _borderVertices.AddCoordinate(bottomRight);
           
            _fillVertices.Color = borderColor;
            _fillVertices.AddCoordinate(topLeft);
            _fillVertices.AddCoordinate(topRight);
            _fillVertices.AddCoordinate(bottomLeft);
            _fillVertices.AddCoordinate(bottomRight);


            _fillIndices = new IndexCollection(1);

            List<int> values = new List<int>();
            values.Add(0);
            values.Add(1);
            values.Add(1);
            values.Add(3);
            values.Add(3);
            values.Add(2);
            values.Add(2);
            values.Add(0);
            _borderIndices = new IndexCollection(values);
            IsInitialized = true;

        }

        /// <summary>
        /// Occurs immediately before drawing, allowing for the drawing configuration to be updated.
        /// </summary>
        /// <param name="e">A DrawVerifyArgsDX event argument</param>
        /// <returns>Boolean, true if drawing should be canceled</returns>
        protected override bool OnBeforeDrawing(DrawVerifyArgsDX e)
        {
            if (_fillVertices != null)
            {
                if (e.Camera.DrawBox != _fillVertices.DrawBox)
                {
                    _fillVertices.Invalidate();
                }
            }

            if (_borderVertices != null)
            {
                if (e.Camera.DrawBox != _borderVertices.DrawBox)
                {
                    _borderVertices.Invalidate();
                }
            }

            IsInitialized = false;
             
            return base.OnBeforeDrawing(e);
        }
       

        /// <summary>
        /// 
        /// </summary>
        public PolygonSymbolizer Symbolizer
        {
            get { return _symbolizer; }
            set { _symbolizer = value; }
        }

        #endregion

      
    }
}
